package com.jmlee.service.impl;

import com.jmlee.mapper.RoleMapper;
import com.jmlee.pojo.Role;
import com.jmlee.service.RoleService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public Integer addRole(Role role) {
        return roleMapper.insertRole(role);
    }

    @Override
    public Integer editRoleById(Role role) {
        return roleMapper.updateRole(role);
    }

    @Override
    public List<Role> roleList() {
        return roleMapper.selectRoleList();
    }

    @Override
    public List<Role> roleList(RowBounds rowBounds) {
        return roleMapper.selectRoleList(rowBounds);
    }

    @Override
    public List<Role> getRoleList(Role role) {
        List<Role> list=roleMapper.getRoleList(role);
        return list;
    }

    @Override
    public Integer addAuthority(Long rId, Long mId) {
        Integer state = 0;
        //判断该权限是否已经存在
        Integer exits = roleMapper.selectAuthorityByRole(rId,mId);
        System.out.println(exits);
        //如果不存在，则执行insert方法
        if (exits == 0){
            state = roleMapper.insertAuthorityByRole(rId,mId);
        }else {
            //如果存在，则执行update方法
            state = roleMapper.updateAuthorityById(rId,mId,1);
        }
        return state;
    }

    @Override
    public Integer removeAuthority(Long rId, Long mId) {
        Integer state = 0;
        state = roleMapper.updateAuthorityById(rId,mId,0);
        return state;
    }

    @Override
    public List<Long> getMenuIdListByRid(Long rId) {
        return roleMapper.selectMenuIdByRid(rId);
    }

    @Override
    public Long getCountOfRole() {
        return roleMapper.selectRoleCount();
    }

    @Override
    public Integer deleteRole(Role role) {
        return roleMapper.deleteRoleById(role);
    }

    @Override
    public Integer checkRoleName(Role role) {
        return roleMapper.selectRoleByName(role);
    }
}
